package org.boyan.modules.controller;

import com.alibaba.fastjson.JSON;
import org.boyan.common.api.vo.Result;
import org.boyan.entity.blood.BloodAnalysis;
import org.boyan.entity.blood.TableInfo;
import org.boyan.modules.service.BloodAnalysisService;
import org.boyan.modules.service.TableInfoService;
import org.boyan.modules.vo.BloodVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/blood")
public class BloodAnalysisController {
    @Autowired
    private BloodAnalysisService bloodAnalysisService;

    @Autowired
    private TableInfoService tableInfoService;

    @PostMapping("/getBloodList")
    public Result<?> getBloodList(@RequestBody Map<String,Object> map){
        int currentPage= (int) map.get("currentPage");
        int pageSize= (int) map.get("pageSize");
        Map<String,Object> bloodMap= (Map<String, Object>) map.get("blood");
        BloodAnalysis bloodAnalysis= JSON.parseObject(JSON.toJSONString(bloodMap),BloodAnalysis.class);
        List<BloodVO> bloodVOList=bloodAnalysisService.getBloodList(bloodAnalysis,currentPage,pageSize);
        int total=bloodAnalysisService.getTotal(bloodAnalysis);
        Map<String,Object> retMap=new HashMap<>();
        retMap.put("bloodList",bloodVOList);
        retMap.put("total",total);
        return Result.OK(retMap);
    }

    @PostMapping("/getTableInfo")
    public Result getTableInfo(@RequestBody Map<String,Object>map){
        String sourceTable= (String) map.get("sourceTable");
        String databaseName= (String) map.get("databaseName");
        int current= (int) map.get("current");
        int size= (int) map.get("size");
        Map<String,Object> tableMap= (Map<String, Object>) map.get("tableform");
        TableInfo tableInfo=JSON.parseObject(JSON.toJSONString(tableMap),TableInfo.class);
        tableInfo.setDatabaseName(databaseName);
        tableInfo.setTableName(sourceTable);
        List<TableInfo> tableInfos=tableInfoService.getTableInfoListByCriteria(current,size,tableInfo);
        int total=tableInfoService.getTotalByCriteria(tableInfo);
        Map<String,Object> retMap=new HashMap<>();
        retMap.put("tableInfos",tableInfos);
        retMap.put("total",total);
        return Result.OK(retMap);
    }
}
